package com.tron.sync.elk;

import com.alibaba.fastjson.JSONObject;
import com.tron.sync.entities.SyndaOrg;
import com.tron.sync.entities.SyndaOrgDoc;
import com.tron.sync.entities.SyndaUser;
import com.tron.sync.entities.SyndaUserDoc;
import com.tron.sync.service.ElkService;
import com.tron.sync.service.SyndaOrgService;
import com.tron.sync.service.SyndaUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class AmqpMessageReceive {
    @Autowired
    SyndaOrgService syndaOrgService;

    @Autowired
    SyndaUserService syndaUserService;

    @Autowired
    ElkService elkService;

    @RabbitListener(queues = "syncService")
    public void process(String string) {
        try {
            log.info("======收到queue OrgUserChangeNotice消息：======{}", string);
            AmqpMessage amqpMessage = JSONObject.parseObject(string, AmqpMessage.class);
            // 获取前3秒时间
            final Date time = amqpMessage.getTime();
            final Instant instant = time.toInstant();
            final LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            final LocalDateTime minusDays = localDateTime.minusSeconds(3);
            final Instant toInstant = minusDays.toInstant(ZoneOffset.ofHours(8));
            final Date from = Date.from(toInstant);
            // 更新数据到全文检索
            sync(from);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void sync(Date date) {
        // 查询synda库中用户数据
        final List<SyndaUser> syndaUsers = syndaUserService.findByTime(date);

        // 全文检索中更新用户数据
        final ArrayList<SyndaUserDoc> syndaUserDocs = new ArrayList<>();
        syndaUsers.forEach(i -> {
            final SyndaUserDoc syndaUserDoc = new SyndaUserDoc();
            syndaUserDoc.setId(i.getId());
            syndaUserDoc.setName(i.getName());
            syndaUserDoc.setGender(i.getGender());
            syndaUserDoc.setStatus(i.getStatus());
            syndaUserDoc.setUserMobile(i.getMobile());
            syndaUserDoc.setTelephone(i.getTelephone());
            syndaUserDoc.setInsidePhone(i.getInsidePhone());
            syndaUserDoc.setJob(i.getJob());
            syndaUserDoc.setJobRank(i.getJobRank());
            syndaUserDoc.setUserNumber(i.getUserNumber());
            syndaUserDoc.setCategory(i.getCategory());
            syndaUserDoc.setUserType(i.getType());
            syndaUserDoc.setPinyin(i.getPinyin());
            syndaUserDoc.setOrgId(i.getGroupId());
            syndaUserDoc.setOrgType(i.getOrgType());
            syndaUserDoc.setPath(i.getPath());
            syndaUserDoc.setOrgName(i.getOrgName());
            try {
                syndaUserDoc.setHireDate(i.getHireDate().toInstant().toEpochMilli());
                syndaUserDoc.setJoinPartyDate(i.getJoinPartyDate().toInstant().toEpochMilli());
            } catch (Exception e) {

            }
            syndaUserDoc.setUserSort(i.getSort());
            syndaUserDoc.setUserIdentity(i.getUserIdentity());
            syndaUserDocs.add(syndaUserDoc);
        });
        elkService.saveUser(syndaUserDocs);

        // 查询synda库中机构数据
        final List<SyndaOrg> syndaOrgs = syndaOrgService.findByTime(date);

        // 全文检索中更新机构数据
        final ArrayList<SyndaOrgDoc> syndaOrgDocs = new ArrayList<>();
        syndaOrgs.forEach(i->{
            final SyndaOrgDoc syndaOrgDoc = new SyndaOrgDoc();
            syndaOrgDoc.setId(i.getId());
            syndaOrgDoc.setName(i.getName());
            syndaOrgDoc.setOrgAlias(i.getOrgAlias());
            syndaOrgDoc.setStatus(i.getStatus());
            syndaOrgDoc.setLevelId(i.getLevelId());
            syndaOrgDoc.setRoleIds(i.getRoleIds());
            syndaOrgDoc.setOrgType(i.getType());
            syndaOrgDoc.setAttribute(i.getAttribute());
            syndaOrgDoc.setOrgPid(i.getOrgPid());
            syndaOrgDoc.setOrgCode(i.getOrgCode());
            syndaOrgDoc.setRegion(i.getRegion());
            syndaOrgDoc.setBrevity(i.getBrevity());
            syndaOrgDoc.setInherit(i.getInherit());
            syndaOrgDoc.setSort(i.getSort());
            syndaOrgDoc.setClassIfy(i.getClassIfy());
            syndaOrgDoc.setPath(i.getPath());
            syndaOrgDocs.add(syndaOrgDoc);
        });
        elkService.saveOrg(syndaOrgDocs);
    }
}
